<!DOCTYPE html>
[#escape x as (x)!?html]
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <title>用户登录 - ${site.seoTitle} - Powered by JPROCMS</title>
    [#include '../includes/common_res.html'/]
    <script src="${resource}/static/plugins/crypto-js/crypto-js.min.js"></script>
    <script src="${resource}/static/plugins/sm-crypto/dist/sm2.js"></script>
  </head>
  <body class="bg-light">
    <div class="container my-3 my-md-4 my-lg-5">
      <div class="row justify-content-center">
        <div class="col col-md-6 col-lg-5 col-xl-5 reg-box">
          <h3 class="reg-logo text-center">
            <a href="/"><b>JPROCMS</b></a> <span>登录</span>
          </h3>
          <div
            id="tipDialog"
            class="alert alert-warning alert-dismissible fade show d-none"
            role="alert"
          >
            <button
              type="button"
              class="close"
              data-dismiss="alert"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <span id="tipMsg"></span>
          </div>

          <div
            class="reg-box-body rounded p-4 mt-4"
            style="background-color: #fff; border: 1px solid #ccc"
          >
            <form class="mb-2" id="validForm" action="" method="post">
              <div class="form-group">
                <input
                  type="text"
                  class="form-control"
                  id="username"
                  name="username"
                  placeholder="用户名"
                  title="请输入用户名"
                  required
                />
                <label class="sr-only" for="username">用户名</label>
                <!--<small class="form-text text-muted">4~18个字符，可使用字母、数字、下划线</small>-->
              </div>
              <div class="form-group">
                <input
                  type="password"
                  class="form-control"
                  id="password"
                  name="password"
                  placeholder="密码"
                  title="请输入密码"
                  required
                  autocomplete="off"
                />
                <label class="sr-only" for="password">密码</label>
              </div>
              <button type="submit" class="btn btn-primary btn-block">
                登录
              </button>
              <div class="text-center mt-2">
                <small
                  ><a href="${base}/register">注册</a
                  ><a class="ml-3" href="${base}/pwd-reset"
                    >忘记密码</a
                  ></small
                >
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>

    <script>
      var publicKey = "";
      axios
        .get("${api}/user/get-client-public-key", {
          headers: { "Tenant-Id": "1" },
        })
        .then(function (response) {
          publicKey = response.data.data;
        });

      function sm2Encrypt(message) {
        // 1 - C1C3C2，0 - C1C2C3，默认为1
        const cipherMode = 1;
        let encryStr = sm2.doEncrypt(message, publicKey, cipherMode);
        return encryStr;
      }

      $(function () {
        $("#username").focus().select();
        $("#validForm").validate({
          rules: {
            captcha: {
              remote: {
                url: "${api}/captcha/try",
                data: {
                  token: function () {
                    return $("#captchaToken").val();
                  },
                },
              },
            },
          },
          submitHandler: function (form, event) {
            event.preventDefault();
            var $password = $("#password");
            let ensPsw = sm2Encrypt($password.val());
            var formData = JSON.stringify({
              username: $("#username").val(),
              password: ensPsw,
            });
            $.ajax({
              url: "${api}/user/cmslogin", // 目标URL
              type: "POST", // 请求类型
              headers: {
                "Tenant-Id": Cms.getTenantId(),
                "content-type": "application/json",
              },
              data: formData,
              success: function (response) {
                //登录成功
                if (response.code == 0) {
                  $("#tipMsg").text("登录成功");
                  sessionStorage.setItem(
                    "accessToken",
                    response.data.accessToken
                  );
                  sessionStorage.setItem(
                    "expiresTime",
                    response.data.expiresTime
                  );
                  sessionStorage.setItem(
                    "refreshToken",
                    response.data.refreshToken
                  );
                  sessionStorage.setItem("userId", response.data.userId);
                  sessionStorage.setItem("avatar", response.data.avatar);
                  sessionStorage.setItem("deptName", response.data.deptName);
                  sessionStorage.setItem("nickname", response.data.nickname);
                  sessionStorage.setItem("username", response.data.username);
                  window.location.href = "${base}/";
                } else {
                  $("#tipMsg").text(response.msg);
                }
                $("#tipDialog").removeClass("d-none");
              },
              error: function (xhr, status, error) {
                // 请求失败时的回调函数
                console.error(error);
                $("#tipMsg").text("服务异常,请联系管理员");
                $("#tipDialog").removeClass("d-none");
              },
            });
          },
        });
      });
    </script>
  </body>
</html>
[/#escape]
